<?php
// leetcode 150题求逆波兰表达式的值
class Solution {

    /**
     * @param String[] $tokens
     * @return Integer
     */
    function evalRPN($tokens) {
        $stk = new SplStack();
        foreach ($tokens as $s) {
            switch ($s) {
                case "+":
                    $num1 = $stk->pop();
                    $stk->push($stk->pop() + $num1);
                    break;
                case "-":
                    $num1 = $stk->pop();
                    $stk->push($stk->pop() - $num1);
                    break;
                case "*":
                    $num1 = $stk->pop();
                    $stk->push($stk->pop() * $num1);
                    break;
                case "/":
                    $num1 = $stk->pop();
                    $stk->push(intval($stk->pop() / $num1));
                    break;
                default:
                    $stk->push(intval($s));
                    break;
            }
        }
        return $stk->pop();
    }
}

$solution = new Solution();
$res = $solution->evalRPN(["10","6","9","3","+","-11","*","/","*","17","+","5","+"]);
echo $res;